package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.DelReq;
import com.cloud.lan.api.dto.req.LanUserFavouritesReq;
import com.cloud.lan.dao.LanUserFavouritesMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.enums.LanError;
import com.cloud.lan.entity.model.dataobject.LanUserFavouritesDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanUserFavouritesService;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;

/**
 * <p>
 * 用户收藏表 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2022-05-01
 */
@Slf4j
@Service
public class LanUserFavouritesServiceImpl extends ServiceImpl<LanUserFavouritesMapper, LanUserFavouritesDO> implements ILanUserFavouritesService {

    @Resource
    LanUserFavouritesMapper lanUserFavouritesMapper;

    @Override
    public BaseResponse manageList(LanUserFavouritesReq req) {
        log.info("#查询 用户收藏 列表 {}", req);
        LambdaQueryWrapper<LanUserFavouritesDO> qw = new LambdaQueryWrapper<>();
        qw.eq(LanUserFavouritesDO::getIsDeleted, false).orderByDesc(LanUserFavouritesDO::getCreateTime);
        Page<LanUserFavouritesDO> page = new Page<>(req.getPageIndex(), req.getPageSize());
        IPage<LanUserFavouritesDO> pageList = lanUserFavouritesMapper.selectPage(page, qw);
        return BaseResponse.success(pageList);
    }

    @Override
    public BaseResponse add(LanUserFavouritesReq req) {
        log.info("#新增 用户收藏表 {}", req);
        LanUserFavouritesDO lanUserFavouritesDO = new LanUserFavouritesDO();
        BeanUtil.copyProperties(req, lanUserFavouritesDO);
        lanUserFavouritesMapper.insert(lanUserFavouritesDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse update(LanUserFavouritesReq req) {
        log.info("#更新 用户收藏表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId())},
                new String[]{"id"}, LanError.ILLEGAL_PARAMS.getCode());
        LanUserFavouritesDO lanUserFavouritesDO = lanUserFavouritesMapper.selectById(req.getId());
        BeanUtil.copyProperties(req, lanUserFavouritesDO);
        lanUserFavouritesDO.setUpdateTime(DateUtil.date());
        lanUserFavouritesMapper.updateById(lanUserFavouritesDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse delete(DelReq req) {
        log.info("#删除 用户收藏表 {}", req);
        VocUtil.checkNullAttribute(new String[]{String.valueOf(req.getId())}, new String[]{"id"}, LanError.ILLEGAL_PARAMS.getCode());
        LambdaUpdateWrapper<LanUserFavouritesDO> luw = new LambdaUpdateWrapper<>();
        luw.set(LanUserFavouritesDO::getIsDeleted, ONE.getKey())
                .set(LanUserFavouritesDO::getUpdateTime, DateUtil.date())
                .eq(LanUserFavouritesDO::getId, req.getId());
        lanUserFavouritesMapper.update(new LanUserFavouritesDO(), luw);
        return BaseResponse.success();
    }
}
